C  MODPATH release: Version 4.00 (V4, Release 1, 2-2000)
C    Changes to work with MODFLOW-2000
C
C MODPATH Version 3.00 (V3, Release 2, 5-99)
C Changes:
C   No change from previous release: (V3, Release 1, 9-94)
C***** SUBROUTINES *****
C     SPACE
C     FILES
C***********************
 
C***** SUBROUTINE *****
      SUBROUTINE SPACE(LENA,LCQX,LCQY,LCQZ,LCPOR,LCIBOU,LCXMAX,LCDX,
     1LCYMAX,LCDY,LCZBOT,LCZTOP,LCHEAD,LCBUFF,LCLAYC,LCNCON,
     2LCIBUF,LCXLC,LCQSS,LCYLC,LCZLC,LCZLL,LCTOT,LCJLC,LCILC,
     3LCKLC,LCQSTO,
     4LCINI,LCPERL,LCNTS,LCTMX,LCIBST,IUNIT,HDRY,HNOFLO,
     5MAXPTS,ISUM,LAYCBD)
C
      INCLUDE 'idat1.inc'
      COMMON /IDAT2/ IRCHTP,IEVTTP,NRCHOP,NEVTOP
      COMMON /MAXSIZ/ MAXSIZ
      DIMENSION IUNIT(NUNIT)
      DIMENSION LAYCBD(200)
      CHARACTER*133 LINE
      INTEGER*4 MAXSIZ,MX
C
      IU=IUNIT(10)
C
C  MF-2000 DIS data
      ISUM=1
      CALL DIS2AL(IUNIT(1),I7,LAYCBD,NCBL,IGRID,NCOL,NROW,NLAY,
     1     NPER)
C
C  ENTER DIMENSION DATA
C
      READ(IU,'(A)') LINE
      ICOL=1
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,2,MX,RDUMMY,I7,IU)
      IF(MX.GT.0) MAXSIZ=MX
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,3,IDUMMY,HNOFLO,I7,IU)
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,3,IDUMMY,HDRY,I7,IU)
      IF(LINE(IWSTRT:IWLAST).EQ.' ') THEN
        WRITE(*,*) 'You must enter a value for HDRY in the main data fil
     1e. Stop.'
		call stopfile  ! emrl
        STOP
      END IF
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,2,MAXNP,RDUMMY,I7,IU)
      IF(LINE(IWSTRT:IWLAST).EQ.' ' .OR. MAXNP.LE.0) MAXNP=MAXPTS
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,2,IRCHTP,RDUMMY,I7,IU)
      IF(LINE(IWSTRT:IWLAST).EQ.' ') IRCHTP=0
      IF(IRCHTP.NE.0) IRCHTP=1
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,2,IEVTTP,RDUMMY,I7,IU)
      IF(LINE(IWSTRT:IWLAST).EQ.' ') IEVTTP=0
      IF(IEVTTP.NE.0) IEVTTP=1
      WRITE(I7,*) IRCHTP,IEVTTP
C 
C
      NCRL=NCOL*NROW*NLAY
      NCP1=NCOL+1
      NRP1=NROW+1
      NLP1=NLAY+1
      NLPOR= NLAY+NCBL
C
      IF(IGRID.EQ.1) THEN
      NZDIM=NLAY
      ELSE
      NZDIM=NCRL
      END IF
C
      LCQX=ISUM
      ISUM=ISUM+NCRL+(NROW*NLAY)
      LCQY=ISUM
      ISUM=ISUM+NCRL+(NCOL*NLAY)
      LCQZ=ISUM
      ISUM=ISUM+NCRL+(NCOL*NROW)
      LCPOR=ISUM
      ISUM=ISUM+(NCOL*NROW*NLPOR)
      LCIBOU=ISUM
      ISUM=ISUM+NCRL
      LCIBST=ISUM
      ISUM=ISUM+NCRL
      LCXMAX=ISUM
      ISUM=ISUM+NCOL
      LCDX=ISUM
      ISUM=ISUM+NCOL
      LCYMAX=ISUM
      ISUM=ISUM+NROW
      LCDY=ISUM
      ISUM=ISUM+NROW
      LCZBOT=ISUM
      ISUM=ISUM+NZDIM
      LCZTOP=ISUM
      ISUM=ISUM+NZDIM
      LCHEAD=ISUM
      ISUM=ISUM+NCRL
      LCBUFF=ISUM
      ISUM=ISUM+NCRL
      LCLAYC=ISUM
      ISUM=ISUM+NLAY
      LCNCON=ISUM
      ISUM=ISUM+NLAY
      LCIBUF=ISUM
      ISUM=ISUM + NCRL
      LCQSS=ISUM
      ISUM=ISUM+NCRL
      LCQSTO=ISUM
      ISUM=ISUM + NCRL
      LCPERL=ISUM
      ISUM=ISUM + NPER
      LCNTS=ISUM
      ISUM=ISUM + NPER
      LCTMX=ISUM
      ISUM=ISUM + NPER
C
      ISM1=ISUM-1
      NDIF=LENA-ISM1
      NPART=NDIF/9
      IF(NPART.GT.MAXNP) NPART=MAXNP
      IF (NPART.LT.1) THEN
      IF (ISILNT.EQ.0) THEN
      WRITE (*,*)
     1'THE MAXIMUM SIZE OF THE MASTER ARRAY MUST BE INCREASED.'
      WRITE (*,*)
     1'REDIMENSION THE MASTER ARRAY:  A(LENA)'
      WRITE (*,5010) ISM1
5010  FORMAT(' WITH   LENA =',I8,' + 9 X (NUMBER OF PARTICLES)')
      END IF
      WRITE (I7,*)
     1'THE MAXIMUM SIZE OF THE MASTER ARRAY MUST BE INCREASED.'
      WRITE (I7,*)
     1'REDIMENSION THE MASTER ARRAY:  A(LENA)'
      WRITE (I7,5010) ISM1
		call stopfile  ! emrl
      STOP
      END IF
      LCXLC=ISUM
      ISUM=ISUM+NPART
      LCYLC=ISUM
      ISUM=ISUM+NPART
      LCZLC=ISUM
      ISUM=ISUM+NPART
      LCZLL=ISUM
      ISUM=ISUM+NPART
      LCJLC=ISUM
      ISUM=ISUM+NPART
      LCILC=ISUM
      ISUM=ISUM+NPART
      LCKLC=ISUM
      ISUM=ISUM+NPART
      LCTOT=ISUM
      ISUM=ISUM+NPART
      LCINI=ISUM
      ISUM=ISUM+NPART
      ISM1=ISUM-1
      IF (IBATCH.EQ.0) THEN
      WRITE (*,5020) NPART
5020  FORMAT (' THE MAXIMUM NUMBER OF PARTICLES FOR THIS RUN IS ',
     1 I8)
      END IF
      WRITE (I7,5030) NPART
5030  FORMAT (' MAXIMUM NUMBER OF PARTICLES IS ',I8)
      WRITE (I7,5040) ISM1,LENA
5040  FORMAT (I8,' ELEMENTS OUT OF ',I8,' USED IN THE "A" ARRAY')
      RETURN
      END
 
C***** SUBROUTINE *****
      SUBROUTINE FILES (I0,I1,I7,IBATCH,IUNIT,CUNIT,NUNIT,
     1                  IUNAR,NIUNAR,VER)
      DIMENSION IUNIT(NUNIT),IUNAR(NIUNAR)
      CHARACTER*256 FNAME,LINE,FNAMS, path  ! emrl 80 to 256
      CHARACTER*25 CUNIT(NUNIT),STRING
      CHARACTER*1 INLINE
      CHARACTER*(*) VER
      COMMON /DFNAMS/ FNAMS(6)
	common /emrlpath/ path  ! emrl jig

C
C  CREATE AND OPEN STANDARD OUTPUT AND SCRATCH FILES
C
      OPEN(I7,STATUS='SCRATCH')
      INLINE= '@'
C
C  OPEN FILE CONTAINING FILE NAMES AND FORTRAN UNITS
C
      CALL MPRMPT
     1('ENTER THE NAME FILE:')
      CALL GETSTR(FNAME,-1,'4.2.1')
	if (path(1:1).eq.' ') then  ! emrl jig
	  call getpath(fname,path)  ! emrl jig
	endif                       ! emrl jig
	call setpath(path, fname) ! emrl mjk
      CALL OPNFIL (I0,FNAME,1,I7,IBATCH,1)
      CALL PUTSTR(FNAME)
C
9     READ (I0,'(A256)',END=20 ) LINE  ! emrl 80 to 256
      IF(LINE(1:1).EQ.INLINE .OR. LINE.EQ.' ') GO TO 9
      ICOL=1
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,1,NDUMMY,RDUMMY,I7,I0)
      STRING= LINE(IWSTRT:IWLAST)
      IF(STRING.EQ.'LIST') THEN
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,2,IU,RDUMMY,I7,I0)
      IF(IU.LT.0) IU= -IU
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,0,NDUMMY,RDUMMY,I7,I0)
      FNAME= LINE(IWSTRT:IWLAST)
      CLOSE(I7)
      I7=IU
	call setpath(path, fname) ! emrl mjk
      ELSE
      FNAME= 'summary.pth'
	call setpath(path, fname) ! emrl mjk
      REWIND(I0)
      CLOSE(I7)
      END IF
      CALL OPNFIL (I7,FNAME,4,I7,IBATCH,3)
C
C  WRITE VERSION NUMBER IN THE LISTING FILE
C
      WRITE(I7,'(A/)') VER
 
10    READ (I0,'(A256)',END=20 ) LINE  ! emrl 80 to 256
      IF(LINE(1:1).EQ.INLINE .OR. LINE.EQ.' ') GO TO 10
      ICOL=1
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,1,NDUMMY,RDUMMY,I7,I0)
      STRING= LINE(IWSTRT:IWLAST)
      NVALID=1
      DO 11 N=1,NUNIT
      IF(STRING.EQ.CUNIT(N)) NVALID=0
      IF(STRING.EQ.'DATA') NVALID=0
11    CONTINUE
      IF(NVALID.EQ.1) THEN
      CALL CHOP(STRING,L)
      WRITE(I7,5100) STRING(1:L)
5100  FORMAT(' SKIPPING UNRECOGNIZED FILE TYPE: ',A)
      GO TO 10
      END IF
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,2,IU,RDUMMY,I7,I0)
      IF(IU.LT.0) IU= -IU
      CALL URWORD(LINE,ICOL,IWSTRT,IWLAST,0,NDUMMY,RDUMMY,I7,I0)
      FNAME= LINE(IWSTRT:IWLAST)
      call setpath(path, FNAME) ! emrl mjk
 
C... CHECK FOR RESERVED UNIT NUMBERS
      IUTMP=IU
      IF(IUTMP.LT.0) IUTMP= -IUTMP
      DO 5 N=1,NIUNAR
      IF(IUTMP.EQ.IUNAR(N)) THEN
      WRITE(I7,*) 'ERROR IN NAME & UNIT NUMBER FILE ON LINE:'
      WRITE(I7,'(1X,A)') LINE
      WRITE(I7,6000) IUTMP
      WRITE(I7,*)
      WRITE(I7,*) 'MODPATH RESERVES THE FOLLOWING UNIT NUMBERS:'
      WRITE(I7,'(20I4)') (IUNAR(K),K=1,NIUNAR)
		call stopfile  ! emrl
      STOP
      END IF
6000  FORMAT(1X,'UNIT NUMBER ',I3,' IS RESERVED. PICK ANOTHER NUMBER.')
5     CONTINUE
 
      IF(STRING.EQ.'MAIN') I1=IU
      DO 1 N=1,NUNIT
      IF(STRING.EQ.CUNIT(N)) THEN
      IUNIT(N)=IU
      GO TO 2
      END IF
1     CONTINUE
2     CONTINUE
C... MAKE SURE UNIT NUMBER IS FLAGGED AS NEGATIVE FOR UNFORMATTED FILES
      IF(STRING.EQ.'BUDGET') IU= -IU
      IF(STRING.EQ.'HEAD(BINARY)') IU= -IU
C
C  OPEN DATA FILES
C
      CALL CHOP(FNAME,LFNAME)
 
      IF(STRING.EQ.'CBF') THEN
        FNAMS(1)=FNAME
        WRITE(I7,5020) IU,FNAME(1:LFNAME)
      ELSE IF(STRING.EQ.'LOCATIONS') THEN
        FNAMS(2)=FNAME
        WRITE(I7,5020) IU,FNAME(1:LFNAME)
      ELSE IF(STRING.EQ.'TIME') THEN
        FNAMS(3)=FNAME
        WRITE(I7,5020) IU,FNAME(1:LFNAME)
      ELSE IF(STRING.EQ.'ENDPOINT') THEN
        FNAMS(4)=FNAME
        WRITE(I7,5020) IU,FNAME(1:LFNAME)
      ELSE IF(STRING.EQ.'PATHLINE') THEN
        FNAMS(5)=FNAME
        WRITE(I7,5020) IU,FNAME(1:LFNAME)
      ELSE IF(STRING.EQ.'TIME-SERIES') THEN
        FNAMS(6)=FNAME
        WRITE(I7,5020) IU,FNAME(1:LFNAME)
      ELSE
        CALL OPNFIL (IU,FNAME,1,I7,IBATCH,1)
	  CALL CHOP(FNAME,LFNAME) ! emrl
        WRITE(I7,5010) IU,FNAME(1:LFNAME)
      END IF
 
5010  FORMAT(1X,'FILE UNIT NUMBER ',I3,' HAS BEEN CONNECTED TO <',A,'>')
5020  FORMAT(1X,'FILE UNIT NUMBER ',I3,' WILL BE CONNECTED TO <',A,'>')
      GO TO 10
20    CONTINUE
      WRITE(I7,*)
      CLOSE(I0)
      RETURN
      END
